<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class OrderRiskAlipay extends Model
{
    protected $table = "sp_order_risk_alipay";
    protected $primaryKey = 'id';
    public $timestamps = false;

    //查询风控信息
    protected function getRiskData($order_id)
    {
        $data = $this
            ->where('order_id',$order_id)
            ->where("end_at",">",date('Y-m-d H:i:s',time()))
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }
    //商户查询风控信息
    protected function getMemberRiskData($order_id,$member)
    {
        $data = $this
            ->where('order_id',$order_id)
            ->where('member_id',$member)
            ->where("end_at",">",date('Y-m-d H:i:s',time()))
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    /**
     * 风控查询列表
     */
    public static function getRiskAlipayList($page,$pageSize, $where){
        $RiskCredit = OrderRiskAlipay::where($where)
            ->orderBy('id', 'desc')
            ->paginate($pageSize);
//        $RiskCredit = obToArray($RiskCredit);
        foreach ($RiskCredit as $key=>$item) {
            $value=Order::from('sp_order_2 as o')->leftJoin('sp_member_2 as m','o.uid','m.id')->where('m.cert_no',$item['cert_no'])->orderBy('o.id','asc')
                ->select('m.cert_no','o.order_from','m.openId','m.contract_no','o.sale_user_id')->first();
            $source = Source::where(['source_code' => $value['order_from'], 'status' => Source::SOURCE_STATUS_ENABLE])->value('source_name');
            if($value['order_from']=='saleOnline'&&$value['openId']){
                $source=$source.'-微信';
            }
            if($value['order_from']=='saleOnline'&&empty($value['openId'])){
                $source=$source.'-支付宝';
            }
            if($value['contract_no']){
                $source=$source.'-W';
            }
            $channel_name=SaleUser::where('sale_user_id',$value['sale_user_id'])->value('channel_name');
            if($channel_name){
                $source=$source.'-'.$channel_name;
            }
            if($value['sale_user_id']){
                $source=$source.'-'.$value['sale_user_id'];
            }
            $item['source']=$source;
            $item['status']=$item['status']==1?'成功':'失败';
            $item['name']=OtherOrderAllocate::where('member_id',$item['member_id'])->value('name');
            $member=ShopUser::where('member_id',$item['member_id'])->value('shopname');
            $item['name']=$member?$member:$item['name'];

        }
        return $RiskCredit;
    }

    //添加风控信息
    protected function setRiskData($cert_name,$cert_no,$mobile,$data,$status,$result,$userId,$member_id,$reqId)
    {
        $risk_data['cert_name'] = $cert_name;
        $risk_data['cert_no'] = $cert_no;
        $risk_data['mobile'] = $mobile;
        $risk_data['result2'] = $data;
        $risk_data['status'] = $status;
        $risk_data['result'] = json_encode($result);
        $risk_data['user_id'] = $userId;
        $risk_data['req_id'] = $reqId;
        $risk_data['member_id'] = $member_id;
        $risk_data['create_time'] = date('Y-m-d H:i:s',time());

        $this->insert($risk_data);

        return true;
    }

    //编辑风控信息
    protected function editRiskData($order_id,$risk_data)
    {
        $this->where('order_id',$order_id)->update($risk_data);

        return true;
    }

    //添加风控其他信息
    protected function insertRiskData($order_id,$data)
    {
        $risk_data['order_id'] = $order_id;

        $this->insert($data);

        return true;
    }

}
